<!-- 物料 价格信息 -->
<template>
  <div class="clas_All">
    <el-row>
      <el-col :span="24">
        <div class="grid-content bg-purple-dark">
          <el-form :model="AllotList" size="small" :inline="true">
            <!-- <el-form-item prop="">
              <el-select
                v-model="AllotList.deptCode"
                placeholder="部门"
                clearable
              >
                <el-option
                  v-for="dict in sys_normal_disable"
                  :key="dict.deptCode"
                  :label="dict.deptName"
                  :value="dict.deptCode"
                />
              </el-select>
            </el-form-item> -->
            <el-form-item prop="">
              <el-input
                v-model="AllotList.stapCode"
                placeholder="物料编码"
                clearable
              />
            </el-form-item>
            <el-form-item prop="">
              <el-input
                v-model="AllotList.stapName"
                placeholder="物料名称"
                clearable
              />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" size="mini" @click="getBase"
                >查询</el-button
              >
            </el-form-item>
          </el-form>
        </div>
      </el-col>
    </el-row>
    <el-row>
      <el-col>
        <el-table :data="tableData">
          <!-- 使用colunms进行渲染表头  格式:[{label:''},{label:''}，{label:''}]    ，propdata 动态化获取表头    格式['prop1'，'prop2'，'prop3'] -->
          <el-table-column
            v-for="(column, index) in columns"
            :label="column.label"
            :prop="column.prop"
            :key="index"
            align="center"
          >
          </el-table-column>
          <el-table-column align="center" label="操作">
            <template slot-scope="scope">
              <div>
                <el-button
                  size="mini"
                  type="text"
                  v-hasPermi="['bom:price:findPriceByStapCode']"
                  @click="handleLook(scope.row)"
                >
                  查看价格
                </el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="AllotList.pageNum"
          :limit.sync="AllotList.pageSize"
          @pagination="getBase"
        />
      </el-col>
    </el-row>
    <el-dialog
      :visible.sync="inheritAttrs"
      @open="onOpen"
      @close="onClose"
      title="设置初始价格"
    >
      <el-row :gutter="15">
        <el-form
          ref="elForm"
          :model="AllList"
          :rules="rules"
          size="medium"
          label-width="100px"
        >
          <el-row>
            <el-col :span="12">
              <el-form-item label="请选择工作组" prop="">
                <el-select
                  v-model="AllList.deptCode"
                  :style="{ width: '100%' }"
                  clearable
                  @change="selectChange"
                >
                  <el-option
                    v-for="(item, index) in departmentOption"
                    :key="index"
                    :label="item.deptName"
                    :value="item.deptCode"
                    :disabled="item.disabled"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12"> </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="设置价格" prop="field101">
                <el-input
                  v-model="AllList.initPrice"
                  :disabled="isdisabled"
                  clearable
                  :style="{ width: '100%' }"
                >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <!-- <el-button @click="isdisabled = !isdisabled">修改</el-button> -->
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="平均价" prop="field102">
                <el-input
                  v-model="AllList.weightedPrice"
                  clearable
                  disabled
                  :style="{ width: '100%' }"
                >
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="上次采购价" prop="field103">
                <el-input
                  v-model="AllList.lastPrice"
                  clearable
                  disabled
                  :style="{ width: '100%' }"
                >
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-col :span="12">
            <el-form-item label="" prop="field104">
              <el-button type="primary" @click="handleConfirm" size="medium">
                保存
              </el-button>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-button type="primary" size="medium" @click="close">
              取消
            </el-button>
          </el-col>
        </el-form>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import eltab from "@/components/ElTable/index";
import {
  // 获取修改价格
  findPriceByStapCode,
  // 修改数据
  EditPriceByStapCode,
  //
  addBaseQuery,
  enableShop,
} from "@/api/system/information";
import { listDept } from "@/api/system/dept.js";
import { bomDish_bom } from "@/api/system/bom";
export default {
  components: { eltab },
  data() {
    return {
      total: 0,
      inheritAttrs: false,
      isdisabled: true,
      AllotList: {
        pageNum: 1,
        pageSize: 10,
      },
      AllList: {},
      ISTitle: "",
      sys_normal_disable: [],
      columns: [
        { label: "物料编码", prop: "stapCode" },
        { label: "物料名称", prop: "stapName" },
        // { label: "部门名称", prop: "orgName" },
        { label: "基本单位", prop: "stapBaseUnit" },
        { label: "辅助单位", prop: "stapAuxUnit" },
        { label: "物料类型", prop: "stapTypeName" },
        { label: "上下架", prop: "onshelf" },
        { label: "物料规格", prop: "stapSpec" },
        // { label: "最新价格", prop: "lastPrice" },
        // { label: "设置价格", prop: "initPrice" },
        // { label: "权重价格", prop: "weightedPrice" },
      ],
      tableData: [],
      formData: {
        field101: undefined,
        field102: undefined,
        field103: undefined,
        field104: undefined,
        field105: undefined,
      },
      rules: {},
      departmentOption: [],
      stapCode: "",
    };
  },
  methods: {
    handleQuery() {},
    getBase() {
      bomDish_bom(this.AllotList).then((res) => {
        if (res.code == 200) {
          this.total = res.total;
          this.tableData = res.rows;
        }
      });
    },
    selectChange(v) {
      console.log(v);
      this.deptCode = v;
      this.AllList = { deptCode: v };
      findPriceByStapCode({
        stapCode: this.stapCode,
        deptCode: this.deptCode,
      }).then((res) => {
        if (res.code == 200) {
          if (res.data == null) {
            this.isdisabled = false;
          } else if (res.data.lastPrice == null) {
            this.isdisabled = false;
            this.AllList = res.data;
          } else {
            this.AllList = res.data;
          }
          // if (res.data.lastPrice == null) {
          //   this.isdisabled = false;
          //   this.AllList = res.data;
          // } else if(res.data){
          //   this.isdisabled = false;
          // }
        }
      });
    },
    handleLook(v) {
      this.inheritAttrs = true;
      this.stapCode = v.stapCode;
      // this.ISTitle =
      // console.log(v);
      // findPriceByStapCode(v).then((res) => {
      //   if (res.code == 200) {
      //     this.AllList = res.data;
      //   }
      // });
    },
    onOpen() {},
    onClose() {
      console.log(1);
      this.$refs["elForm"].resetFields();
      this.deptCode = "";
      this.stapCode = "";
      this.AllList = {};
    },
    close() {
      this.$emit("update:visible", false);
    },
    handleConfirm() {
      let obj = {
        deptCode: this.deptCode,
        stapCode: this.stapCode,
        initPrice: this.AllList.initPrice,
      };
      EditPriceByStapCode(obj).then((res) => {
        // console.log(res);
        this.$message({
          message: res.msg,
          center: true,
        });
      });
    },
    getenableShop() {
      enableShop().then((res) => {
        if (res.code == 200) {
          this.sys_normal_disable = res.data;
        }
      });
    },
    // 获取部门信息
    getDept() {
      listDept().then((red) => {
        // console.log(red);
        if (red.code == 200) {
          this.departmentOption = red.data;
        } else {
          this.$message({
            message: red.msg,
            type: "warning",
            center: true,
          });
        }
      });
    },
  },
  created() {
    this.getBase();
    this.getDept();
    this.getenableShop();
  },
};
</script>

<style lang="scss" scoped>
.clas_All {
  margin: 20px;
}
</style>
